TOOLCHAIN = arm-elf
LD = $(TOOLCHAIN)-ld
CC = $(TOOLCHAIN)-gcc
AS = $(TOOLCHAIN)-as
OBJCOPY = $(TOOLCHAIN)-objcopy

GCCWARNINGS = -Wall -Wextra -Wshadow \
	      -Wpointer-arith -Wbad-function-cast -Wcast-align \
	      -Wsign-compare -Waggregate-return -Wstrict-prototypes \
	      -Wmissing-prototypes -Wmissing-declarations -Wunused

CFLAGS = -c -Os $(GCCWARNINGS) -mcpu=arm7tdmi-s -march=armv4t -I.
ASFLAGS = 

PROJECT_NAME = GZe_RTOS

ELFFILE = $(PROJECT_NAME).elf
HEXFILE = $(PROJECT_NAME).hex
SRECFILE = $(PROJECT_NAME).srec
BINFILE = $(PROJECT_NAME).bin

OBJECTS = boot.o ZT018.o math.o std_io.o interrupt.o task.o main.o boot_init.o 

BOOTDIR = ./boot/
DRIVERDIR = ./drivers/
DRIVERLCDDIR = ./drivers/lcd/
INCLUDEDIR = ./include/
KERNELDIR = ./kernel/

TARGETS = $(ELFFILE) $(HEXFILE) $(SRECFILE) $(BINFILE)

all: $(TARGETS)

ZT018.o: $(DRIVERLCDDIR)ZT018/Type.h $(DRIVERLCDDIR)ZT018/ZT018.h $(DRIVERLCDDIR)ZT018/ZT018.c
	$(CC) $(CFLAGS) -c -o ZT018.o $(DRIVERLCDDIR)ZT018/ZT018.c

math.o: $(INCLUDEDIR)math.c
	$(CC) $(CFLAGS) -c -o math.o $(INCLUDEDIR)math.c

std_io.o: ZT018.o math.o ./include/std_io.h
	$(CC) $(CFLAGS) -c -o std_io.o ./include/std_io.c

boot.o:	$(BOOTDIR)boot.S
	$(CC) $(CFLAGS) -c -o boot.o $(BOOTDIR)boot.S

interrupt.o: std_io.o $(KERNELDIR)interrupt.c
	$(CC) $(CFLAGS) -c -o interrupt.o $(KERNELDIR)interrupt.c

task.o:	$(KERNELDIR)task.h
	$(CC) $(CFLAGS) -c -o task.o $(KERNELDIR)task.c

main.o: ZT018.o std_io.o interrupt.o task.o $(KERNELDIR)main.c
	$(CC) $(CFLAGS) -c -o main.o $(KERNELDIR)main.c

boot_init.o: interrupt.o main.o $(BOOTDIR)boot_init.c
	$(CC) $(CFLAGS) -c -o boot_init.o $(BOOTDIR)boot_init.c
	

$(ELFFILE): $(OBJECTS) Makefile
	#$(CC) -static -Ttext 0x0 -Tdata 0x40000000 -Tbss 0x40001000 -o -nostartfiles $(ELFFILE) $(OBJECTS)
	$(LD) -EL -static -Ttext 0x0 -Tdata 0x40000000 -Tbss 0x40001000 -Map $(BASENAME).map -o $(ELFFILE) $(OBJECTS)

$(HEXFILE): $(ELFFILE)
	$(OBJCOPY) -w -O ihex --remove-section ".comment" $(ELFFILE) $(HEXFILE)

$(SRECFILE): $(ELFFILE)
	$(OBJCOPY) -O srec -S $(ELFFILE) $(SRECFILE)

$(BINFILE): $(ELFFILE)
	$(OBJCOPY) -O binary -S $(ELFFILE) $(BINFILE)

clean:
	rm -f *.o $(HEXFILE) $(ELFFILE) $(SRECFILE) $(BINFILE)

clean_bits:
	rm -f *.o $(ELFFILE) $(SRECFILE) $(BINFILE)

